<template>
  <el-dialog
    :title="!dataForm.id ? '新增缓存' : '修改缓存'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
      <el-form-item label="缓存键" prop="cacheKey">
        <el-input v-model="dataForm.cacheKey" placeholder="请输入缓存键名"></el-input>
      </el-form-item>
      <el-form-item label="缓存值" prop="cacheValue">
        <el-input v-model="dataForm.cacheValue" placeholder="请输入缓存值" type="textarea" :rows="4"></el-input>
      </el-form-item>
      <el-form-item label="过期时间(秒)" prop="expireTime">
        <el-input-number v-model="dataForm.expireTime" :min="1" :max="86400" label="过期时间(秒)"></el-input-number>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        cacheKey: '',
        cacheValue: '',
        expireTime: 3600
      },
      dataRule: {
        cacheKey: [
          { required: true, message: '缓存键不能为空', trigger: 'blur' }
        ],
        cacheValue: [
          { required: true, message: '缓存值不能为空', trigger: 'blur' }
        ],
        expireTime: [
          { required: true, message: '请输入过期时间', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/cache/cache/queryWormholeGalaxyCache`),
            method: 'post',
            data: this.$http.adornData({
              "id": id
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.cacheKey = data.wmhGalaxyCache.cacheKey
              this.dataForm.cacheValue = data.wmhGalaxyCache.cacheValue
              this.dataForm.expireTime = data.wmhGalaxyCache.expireTime
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/cache/cache/${!this.dataForm.id ? 'createWormholeGalaxyCache' : 'updateWormholeGalaxyCache'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'cacheKey': this.dataForm.cacheKey,
              'cacheValue': this.dataForm.cacheValue,
              'expireTime': this.dataForm.expireTime
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
